package org.example.netty.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.traffic.GlobalTrafficShapingHandler;
import io.netty.handler.traffic.TrafficCounter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ScheduledExecutorService;

/**
 * 流量监控控制器
 */
@Slf4j
public class FlowMonitoringHandler extends GlobalTrafficShapingHandler {

    public FlowMonitoringHandler(ScheduledExecutorService executor, long writeLimit, long readLimit) {
        super(executor, writeLimit, readLimit);
    }

    @Override
    protected void doAccounting(TrafficCounter counter) {
        // log.info("上个监控周期读取了"+counter.lastReadBytes()+"个字节,发送了"+counter.lastWrittenBytes()+"个字节");
        // log.info("读取吞吐量：{}/s", super.trafficCounter.lastReadThroughput());
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        // 连接关闭时，统计多少字节
        log.info("连接关闭，一共读取：{}字节", super.trafficCounter.cumulativeReadBytes());
    }
}
